package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.VesselCleaningHistory;

public class VesselCleaningHistoryPresenter extends AbstractPresenter<VesselCleaningHistory> {
	final String SINGLEITEM = "vesselCleaningHistory";
	final String MULTIITEM = "vesselCleaningHistories";
	
	public ArrayList<VesselCleaningHistory> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public VesselCleaningHistory tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public VesselCleaningHistory createItemFromJSONObject(JSONObject jsonObject){
		VesselCleaningHistory item = new VesselCleaningHistory();
		try{
			item.setVcID(jsonObject.getInt("vcid"));
			item.setVcDate(getDateFromString(jsonObject.getString("VCDate")));
			
			if(!jsonObject.isNull("VCEmpID")){
				//create product if set
				EmployeePresenter presenter = new EmployeePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("VCEmpID");
				item.setVcEmployeeID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setVcEmployeeID(null);
			}
			
			if(!jsonObject.isNull("VCProcedureID")){
				//create product if set
				VesselCleaningProcedurePresenter presenter = new VesselCleaningProcedurePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("VCProcedureID");
				item.setVcProcedureID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setVcProcedureID(null);
			}
			
			if(!jsonObject.isNull("VCVesselID")){
				//create product if set
				VesselPresenter presenter = new VesselPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("VCVesselID");
				item.setVcVesselID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setVcVesselID(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(VesselCleaningHistory item){
		try{
			JSONObject json = new JSONObject();
			json.put("vcid", item.getVcID());
			json.put("VCDate", getDate(item.getVcDate()));

			if(item.getVcEmployeeID()!=null){
				final EmployeePresenter presenter = new EmployeePresenter();
				json.put("VCEmpID", presenter.createJSON(item.getVcEmployeeID()));
			} else {
				json.put("VCEmpID", JSONObject.NULL);
			}

			if(item.getVcProcedureID()!=null){
				final VesselCleaningProcedurePresenter presenter = new VesselCleaningProcedurePresenter();
				json.put("VCProcedureID", presenter.createJSON(item.getVcProcedureID()));
			} else {
				json.put("VCProcedureID", JSONObject.NULL);
			}

			if(item.getVcVesselID()!=null){
				final VesselPresenter presenter = new VesselPresenter();
				json.put("VCVesselID", presenter.createJSON(item.getVcVesselID()));
			} else {
				json.put("VCVesselID", JSONObject.NULL);
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public VesselCleaningHistory getEmptyEntity(){
		VesselCleaningHistory entity = new VesselCleaningHistory();
		return entity;
	}
}
